package cn.sinata.wanglin.village.fragment;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;
import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.adapter.ActiveAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Active;

import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.property.activity.OwnerCertificationActivity;
import cn.sinata.wanglin.utils.RecyclerViewFragmentWithNoAuth;

/**
 * description：小区-->小区动态-->活动
 * Created by kaihongwu on 2016/12/06 上午 9:45
 */
public class ActiveFragment extends RecyclerViewFragmentWithNoAuth {
    private List<Active> mActives=new ArrayList<Active>();
    private ActiveAdapter mActiveAdapter;
    private int pageNo = 0;
    public static ActiveFragment newInstance(){
        ActiveFragment mFragment = new ActiveFragment();
        return mFragment;
    }
    @Override
    protected RecyclerView.Adapter setAdapter() {
        if(mActiveAdapter==null){
            mActiveAdapter=new ActiveAdapter(mActives,getActivity());
        }
        return mActiveAdapter;
    }

    @Override
    protected void noAuthOnClickListener() {
        super.noAuthOnClickListener();
        ActivityUtil.create(this).go(OwnerCertificationActivity.class)
                .startForResult(12);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 12) {
                pullDownRefresh();
            }
        }
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }
    public void reRefresh(){
        Utils.systemErr("---活动刷新-");
        showDialog();
        pullDownRefresh();
    }
    @Override
    protected void onFirstVisibleToUser() {
        Utils.systemErr("---小区-->小区动态-->活动---");
        getData();
    }

    private void getData() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getHousingDynamicFromActive(userid, MyApplication.houseid,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<Active>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<Active> data) {
                        setRefreshing(false);
                        if("小区未认证！".equals(msg)){
                            setShowAuthView(true);
                        }else {
                            setShowAuthView(false);
                        }
                        if (pageNo == 0) {
                            mActives.clear();
                        }
                        if (data.size() == 0) {
                            if (pageNo == 0) {
                                showToast("暂无数据");
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                showToast("没有更多了");
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mActives.addAll(data);
                        mActiveAdapter.notifyDataSetChanged();

                    }

                    @Override
                    protected void onError(String code, String msg) {
                        super.onError(code, msg);
                        setRefreshing(false);
                    }
                });

    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }
}
